package component.fy.com.common_module.baseAdapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import component.fy.com.common_module.R;
import component.fy.com.common_module.base.BaseCommonApp;
import component.fy.com.common_module.utils.LogUtil;

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    private static final String TAG = "BaseRecyclerAdapter";
    private Context context;
    private List<T> list = null;
    private int layoutId = 0;//布局id
    private int emptyLayoutId = 0;//空布局id
    private LayoutInflater inflater;
    private int itemCount = 0;
    private int itemSelectedPosition = -1;//默认不选中
    public static final int VIEW_LOADING = 10000;
    public static final int VIEW_EMPTY = 10001;
    public static final int VIEW_NORMAL = 10002;
    public static final int VIEW_ERROR = 10003;
    private boolean enableShowEmpty = true;//控制空数据视图的开关
    private boolean enableShowLoading = true;//显示loading的开关
    private boolean enableShowError = false;//控制网络错误的开关
    private OnBaseAdapterItemClick onBaseAdapterItemClick;//所有点击事件

    /**
     * 构造器
     *
     * @param context
     * @param layoutId
     */
    public BaseRecyclerAdapter(Context context, int layoutId) {
        this.context = context;
        this.layoutId = layoutId;
        this.inflater = LayoutInflater.from(context);
    }

    /**
     * 构造器
     *
     * @param context
     * @param layoutId
     * @param emptyLayoutId 自定义的空布局id
     */
    public BaseRecyclerAdapter(Context context, int layoutId, int emptyLayoutId) {
        this.context = context;
        this.layoutId = layoutId;
        this.emptyLayoutId = emptyLayoutId;
        this.inflater = LayoutInflater.from(context);
    }

    public Context getContext() {
        return context;
    }

    /**
     * 点击事件
     *
     * @param onBaseAdapterItemClick
     */
    public void setOnBaseAdapterItemClick(OnBaseAdapterItemClick onBaseAdapterItemClick) {
        this.onBaseAdapterItemClick = onBaseAdapterItemClick;
    }

    /**
     * 设置是否显示空布局 建议在初始化adapter之后调用
     *
     * @param enableShowEmpty
     */
    public void setEnableShowEmpty(boolean enableShowEmpty) {
        this.enableShowEmpty = enableShowEmpty;
    }

    /**
     * 设置显示网络错误的布局
     *
     * @param enableShowError
     */
    public void setEnableShowError(boolean enableShowError) {
        this.enableShowError = enableShowError;
        notifyDataSetChanged();
    }

    /**
     * 设置loading布局的显示与隐藏
     *
     * @param enableShowLoading
     */
    public void setEnableShowLoading(boolean enableShowLoading) {
        this.enableShowLoading = enableShowLoading;
    }

    /**
     * 获取选中item
     *
     * @return
     */
    public int getItemSelectedPosition() {
        return itemSelectedPosition;
    }

    /**
     * 设置选中item
     *
     * @param itemSelectedPosition
     */
    public void setItemSelectedPosition(int itemSelectedPosition) {
        this.itemSelectedPosition = itemSelectedPosition;
    }

    @Override
    public int getItemViewType(int position) {
        if (enableShowError && (list == null || list.size() == 0)) {
            return VIEW_ERROR;
        }
        if (!enableShowError) {
            if (list == null && enableShowLoading) {
                return VIEW_LOADING;
            }
            if (list.size() == 0 && enableShowEmpty && list != null) {
                return VIEW_EMPTY;
            }
        }
        /*if (enableShowLoading && (list == null)) {
            return VIEW_LOADING;
        }*/
        /*if (enableShowEmpty && list != null && list.size() == 0) {
            return VIEW_EMPTY;
        }*/
        return VIEW_NORMAL;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        LogUtil.e(TAG, "viewType = " + viewType);
        View view = null;
        if (viewType == VIEW_LOADING) {//加载布局
            view = inflater.inflate(R.layout.common_base_adapter_loading_view, parent, false);
        } else if (viewType == VIEW_EMPTY) {//没有数据
            if (emptyLayoutId == 0) {
                view = inflater.inflate(R.layout.common_base_adapter_empty_view, parent, false);
            } else {
                view = inflater.inflate(emptyLayoutId, parent, false);
            }
        } else if (viewType == VIEW_NORMAL) {//正常有数据布局
            view = inflater.inflate(layoutId, parent, false);
        } else if (viewType == VIEW_ERROR) {//网络错误
            view = inflater.inflate(R.layout.common_base_adapter_error_view, parent, false);
        }
        BaseViewHolder viewHolder = new BaseViewHolder(view);
        return viewHolder;
    }

    @Override
    public int getItemCount() {
        if (enableShowError && (list == null || list.size() == 0)) {
            return 1;
        }
        if (enableShowLoading && list == null) {
            return 1;
        }
        if (enableShowEmpty && list != null && list.size() == 0) {
            return 1;
        }
        return itemCount;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
        final int itemType = getItemViewType(position);
        if (itemType == VIEW_LOADING) {//加载布局
            ImageView iconLoading = holder.getView(R.id.common_base_adapter_loading_icon);
            Glide.with(BaseCommonApp.getContext())
                    .load(R.drawable.common_loading_eighteen)
                    .into(iconLoading);
        } else if (itemType == VIEW_NORMAL) {//正常布局
            if (list != null) {//这里只是让模拟假数据能显示
                onBind(holder, list.get(position), position);
            } else {
                onBind(holder, null, position);
            }
        } else if (itemType == VIEW_EMPTY) {//空布局

        } else if (itemType == VIEW_ERROR) {//错误布局
            ImageView iconError = holder.getView(R.id.common_base_adapter_error_icon);
        }
        //点击事件
        holder.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onBaseAdapterItemClick != null && itemType == VIEW_NORMAL) {//正常
                    onBaseAdapterItemClick.onItemClick(v, position);
                    onBaseAdapterItemClick.onItemChildClick(v, position);
                } else if (onBaseAdapterItemClick != null && itemType == VIEW_ERROR) {//错误
                    onBaseAdapterItemClick.onErrorViewClick();
                }
            }
        });
        //长按事件
        holder.getItemView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onBaseAdapterItemClick != null && itemType == VIEW_NORMAL) {//正常
                    onBaseAdapterItemClick.onItemLongClick(v, position);
                    onBaseAdapterItemClick.onItemChildLongClick(v, position);
                }
                return false;
            }
        });
    }

    //抽象方法，让子类继承实现
    public abstract void onBind(BaseViewHolder holder, @Nullable T bean, int position);

    /**
     * 设置模拟假数据
     *
     * @param itemCount
     */
    public void setFakeData(int itemCount) {
        this.itemCount = itemCount;
        notifyDataSetChanged();
    }

    /**
     * 设置单个对象数据
     *
     * @param bean
     */
    public void setData(T bean) {
        LogUtil.e(TAG, "设置单个对象数据 list=" + list);
        if (list == null) {
            list = new ArrayList<>();
        } else {
            list.clear();
        }
        list.add(bean);
        itemCount = list.size();
        notifyDataSetChanged();
    }

    /**
     * 设置数据
     *
     * @param list
     */
    public void setData(@NonNull List<T> list) {
        this.list = list;
        this.itemCount = list.size();
        notifyDataSetChanged();
    }

    /**
     * 设置数据
     *
     * @param list
     */
    public void setData(@NonNull List<T> list, int size) {
        this.list = list;
        itemCount = size;
        notifyDataSetChanged();
    }

    /**
     * 添加集合数据
     *
     * @param list
     */
    public void addData(@NonNull List<T> list) {
        if (this.list == null) {
            this.list = new ArrayList<>();
        }
        this.list.addAll(list);
        itemCount = this.list.size();
        notifyDataSetChanged();
    }

    /**
     * 添加一条数据
     *
     * @param bean
     */
    public void addData(@NonNull T bean) {
        if (list == null) {
            list = new ArrayList<T>();
        }
        list.add(bean);
        itemCount = list.size();
        notifyDataSetChanged();
    }

    /**
     * 获取数据集合
     *
     * @return
     */
    public List<T> getData() {
        if (list == null) {
            throw new NullPointerException("BaseRecyclerAdapter here list is null");
        }
        return list;
    }

    /**
     * 清空数据
     */
    public void clearData() {
        if (list != null && list.size() > 0) {
            list.clear();
            this.itemCount = 0;
            notifyDataSetChanged();
        }
    }

    @Override
    public void onViewRecycled(BaseViewHolder holder) {
        super.onViewRecycled(holder);
    }
}
